package cn.tedu.trading_platform.controller;

import cn.tedu.trading_platform.base.response.JsonResult;
import cn.tedu.trading_platform.base.response.StatusCode;
import cn.tedu.trading_platform.mapper.CommentMapper;
import cn.tedu.trading_platform.pojo.dto.CommentInsertParam;
import cn.tedu.trading_platform.pojo.entity.Comment;
import cn.tedu.trading_platform.pojo.vo.CommentVO;
import cn.tedu.trading_platform.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@Api(tags = "评论模块")
@RestController
@RequestMapping("/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @PostMapping("insert")
    @ApiOperation("发表评论功能")
    @ApiOperationSupport(order = 10)
    public JsonResult insertComment(@RequestBody CommentInsertParam commentInsertParam, @ApiIgnore HttpSession session){
        log.debug("commentInsertParam:" + commentInsertParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return new JsonResult(StatusCode.USER_NOT_LOGIN);
        }
        commentInsertParam.setUserId(userVO.getId());
        commentInsertParam.setCreatedAt(new Date());
        commentMapper.insertComment(commentInsertParam);
        return JsonResult.ok();
    }

    @GetMapping("selectByUserId")
    @ApiOperation("查看用户自己的评论信息")
    @ApiOperationSupport(order = 20)
    public JsonResult selectByUserId(HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        List<CommentVO> commentVOS = commentMapper.selectByUserId(userVO.getId());
        return JsonResult.ok(commentVOS);
    }
}

